<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/public/form::head(_)"/>
<body th:replace="admin/public/form::body(~{::div})">
<div class="layui-card">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">图片管理</a>
          <a href="javascript:;" onclick="parent.xadmin.add_tab('首页轮播图管理','/admin/banner/index')">首页轮播图管理</a>
          <a><cite>添加轮播图</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body layui-tab">
        <form id="form1" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">轮播图标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="请填写轮播图标题！"
                           placeholder="请输入轮播图标题" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">封面图</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传封面图</button>
                        <input type="hidden" name="cover" lay-verify="required"  lay-reqtext="请上传图片">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="max-height: 150px">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" name="sorts"
                           placeholder="请输入排序数字" autocomplete="off" class="layui-input " value="99">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">链接</label>
                <div class="layui-input-block">
                    <input type="text" name="url"
                           placeholder="请输入跳转链接" autocomplete="off" class="layui-input " value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <input type="text" name="type"
                           placeholder="请输入轮播图类型" autocomplete="off" class="layui-input " value="">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    layui.use(['jquery', 'form', 'layer', 'upload','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            upload = layui.upload;
        layui.element.render();
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/admin/upload/img?type=banner'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg(res.msg);
                }
                layer.msg('上传成功！');
                $("input[name='cover']").val(res.data.src)
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        //监听提交
        form.on('submit(add)', function (data) {
            //获取标签
            let send_json = data.field;
            //删除空的file
            delete send_json.file;
            $.ajax({
                url: '/admin/banner/add',
                method: 'post',
                data: send_json,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code == 0) {
                        layer.alert("增加成功", {
                            icon: 6
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }

                },
                error: function (data) {
                    layer.msg('服务器繁忙', {icon: 5});
                }
            });
            return false;
        });
    });
</script>
</html>